/*
 * IwoNew.java
 * 
 * Copyright (c) 2012 Supin P Surendran <supinps@gmail.com>.
 * 
 * This file is part of InventoryMan.
 * 
 * InventoryMan is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * InventoryMan is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with InventoryMan.  If not, see <http ://www.gnu.org/licenses/>.
 */

/*
 * IwoNew.java
 *
 * Created on 20 Dec, 2012, 11:17:16 AM
 */

package InventoryMan;

import java.awt.event.ActionEvent;
import java.sql.SQLException;

/**
 *
 * @author Supin P Surendran <supinps@gmail.com>
 */
public class LcrView extends javax.swing.JInternalFrame {

    /** Creates new form IwoView
     * @param conn Connection to the Database
     */
    @SuppressWarnings("static-access")
    public LcrView(java.sql.Connection conn) {
        imConnection=conn;
        print=false;
        comboLcr=new javax.swing.DefaultComboBoxModel();
        TblItemsModel=new javax.swing.table.DefaultTableModel(new Object[]{"Sl No","Product","No of PCS","No of Bundles","Quantity","Remarks"}, 0);
        initComponents();
        ac.decorate(CmbLcr);
        TblLcr.setModel(TblItemsModel);
        initCmbLcr();
        initCmbIwoListener();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        LcrDate = new org.jdesktop.swingx.JXDatePicker();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        TxtCustomer = new javax.swing.JTextArea();
        CmbLcr = new org.jdesktop.swingx.JXComboBox();
        TxtDespatch = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        TxtIwo = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        IwoDate = new org.jdesktop.swingx.JXDatePicker();
        jLabel6 = new javax.swing.JLabel();
        TxtLrNo = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        TxtOrderNo = new javax.swing.JTextField();
        OrderDate = new org.jdesktop.swingx.JXDatePicker();
        jScrollPane2 = new javax.swing.JScrollPane();
        TblLcr = new javax.swing.JTable();
        BnOk = new javax.swing.JButton();
        BnCancel = new javax.swing.JButton();

        setTitle("View LCR");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/icon_small.png"))); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        LcrDate.setEditable(false);

        jLabel3.setText("IWO No :");

        jLabel4.setText("LCR Date :");

        jLabel8.setText("Mode of Despatch :");

        jLabel1.setText("Customer :");

        TxtCustomer.setColumns(20);
        TxtCustomer.setEditable(false);
        TxtCustomer.setRows(5);
        jScrollPane1.setViewportView(TxtCustomer);

        CmbLcr.setModel(comboLcr);

        TxtDespatch.setEditable(false);

        jLabel2.setText("LCR No :");

        TxtIwo.setEditable(false);

        jLabel5.setText("IWO Date :");

        IwoDate.setEditable(false);

        jLabel6.setText("LR/RR No :");

        TxtLrNo.setEditable(false);

        jLabel7.setText("Order No :");

        jLabel9.setText("Order Date :");

        TxtOrderNo.setEditable(false);

        OrderDate.setEditable(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(LcrDate, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(TxtLrNo, javax.swing.GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE)
                    .addComponent(CmbLcr, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(TxtIwo)
                    .addComponent(IwoDate, javax.swing.GroupLayout.DEFAULT_SIZE, 156, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel9, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.TRAILING))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(TxtOrderNo)
                    .addComponent(OrderDate, javax.swing.GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(TxtDespatch)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(22, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(CmbLcr, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(TxtIwo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7)
                            .addComponent(TxtOrderNo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(LcrDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(TxtLrNo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5)
                            .addComponent(IwoDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel9)
                            .addComponent(jLabel8)
                            .addComponent(TxtDespatch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(OrderDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(26, 26, 26))
        );

        TblLcr.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null}
            },
            new String [] {
                "Sl No", "Product", "No of PCS", "No of Bundles", "Quantity", "Remarks"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        TblLcr.getTableHeader().setReorderingAllowed(false);
        jScrollPane2.setViewportView(TblLcr);

        BnOk.setText("Fetch Items");
        BnOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BnOkActionPerformed(evt);
            }
        });

        BnCancel.setText("Cancel");
        BnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BnCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 1179, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(BnOk, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(49, 49, 49)
                        .addComponent(BnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(33, 33, 33)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 231, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(BnCancel)
                    .addComponent(BnOk))
                .addContainerGap(35, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BnOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BnOkActionPerformed
        try {
            if(!print) {
                stmt_items=imConnection.createStatement();
                reply_items=stmt_items.executeQuery("SELECT lcr_item.sl_no,item.name,lcr_item.pcs,lcr_item.bndle_or_roll,lcr_item.remarks " +
                                                    "FROM lcr_item inner join item " +
                                                    "WHERE lcr_item.item_code=item.code AND lcr_item.lcr_id="+comboLcr.getSelectedItem().toString());
                while(reply_items.next()) {
                    int slNo=reply_items.getInt("sl_no");
                    String item=reply_items.getString("name");
                    int pcs=reply_items.getInt("pcs");
                    int bndles=reply_items.getInt("bndle_or_roll");
                    String remarks=reply_items.getString("remarks");
                    TblItemsModel.addRow(new Object[]{slNo,item,pcs,bndles,pcs*bndles,remarks});
                }
                BnOk.setText("Print");
                print=!print;
            }
            else {
                printPrep=new LcrPrintPrep();
                java.text.SimpleDateFormat sd=new java.text.SimpleDateFormat("dd-MM-yyyy");
                printPrep.addEntry(TxtCustomer.getText(), TxtOrderNo.getText(), sd.format(OrderDate.getDate()), TxtDespatch.getText(), CmbLcr.getSelectedItem().toString(), sd.format(LcrDate.getDate()), TxtIwo.getText(), sd.format(IwoDate.getDate()));
                String sl,product,pcs,bndle,qty,remarks;
                for(int i=0;i<TblItemsModel.getRowCount();i++) {
                    sl=TblItemsModel.getValueAt(i, 0).toString();
                    product=TblItemsModel.getValueAt(i, 1).toString();
                    pcs=TblItemsModel.getValueAt(i, 2).toString();
                    bndle=TblItemsModel.getValueAt(i, 3).toString();
                    qty=TblItemsModel.getValueAt(i, 4).toString();
                    remarks=TblItemsModel.getValueAt(i, 5).toString();
                    printPrep.newItem(sl, product, pcs, bndle, qty, remarks);
                }
                printPrep.writeItems();
                printPrep.initPrint();
                this.dispose();
            }
        }catch(java.sql.SQLException ex) {
            javax.swing.JOptionPane.showMessageDialog(this, ex.toString());
        }
    }//GEN-LAST:event_BnOkActionPerformed

    private void BnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BnCancelActionPerformed
        this.dispose();
    }//GEN-LAST:event_BnCancelActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BnCancel;
    private javax.swing.JButton BnOk;
    private org.jdesktop.swingx.JXComboBox CmbLcr;
    private org.jdesktop.swingx.JXDatePicker IwoDate;
    private org.jdesktop.swingx.JXDatePicker LcrDate;
    private org.jdesktop.swingx.JXDatePicker OrderDate;
    private javax.swing.JTable TblLcr;
    private javax.swing.JTextArea TxtCustomer;
    private javax.swing.JTextField TxtDespatch;
    private javax.swing.JTextField TxtIwo;
    private javax.swing.JTextField TxtLrNo;
    private javax.swing.JTextField TxtOrderNo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    // End of variables declaration//GEN-END:variables
    //Manually Declared variables
    org.jdesktop.swingx.autocomplete.AutoCompleteDecorator ac;//To enable autocomplete feature on IWO_NO ComboBox
    javax.swing.DefaultComboBoxModel comboLcr;//Data model that holds iwo ids
    javax.swing.table.DefaultTableModel TblItemsModel;
    boolean print;
    //jdbc...
    java.sql.Connection imConnection;
    java.sql.Statement stmtCmb;
    java.sql.ResultSet replyCmb;
    java.sql.Statement stmt_items;
    java.sql.ResultSet reply_items;
    //Print Preperation Class
    LcrPrintPrep printPrep;
    //End of Declarations

    /**
     * Fetches IWO id's from the database
     * and populates the iwo combobox
     */
    private void initCmbLcr() {
        try {
            stmtCmb=imConnection.createStatement();
            replyCmb=stmtCmb.executeQuery("SELECT lcr_entry.id,lcr_entry.issue_date lcr_date,lcr_entry.lr_no,iwo_entry.id iwo_id,iwo_entry.issue_date iwo_date,indent_entry.order_no,indent_entry.order_date,customer.name,customer.address,iwo_entry.despatch_mode " +
                                          "FROM lcr_entry INNER JOIN iwo_entry INNER JOIN indent_entry INNER JOIN customer " +
                                          "WHERE lcr_entry.iwo_id=iwo_entry.id AND iwo_entry.indent_id=indent_entry.id AND indent_entry.customer_code=customer.code AND lcr_entry.id");
            while(replyCmb.next()) {
                comboLcr.addElement(replyCmb.getInt("id"));
            }
        }catch(java.sql.SQLException ex) {
            javax.swing.JOptionPane.showMessageDialog(this, ex.toString());
        }
    }

    private void initCmbIwoListener() {
        CmbLcr.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(ActionEvent e) {
                try {
                    //Moving cursor to the selected index of the combobox
                    replyCmb.absolute(CmbLcr.getSelectedIndex() + 1);
                    //Populating IWO Info
                    LcrDate.setDate(new java.util.Date(replyCmb.getDate("lcr_date").getTime()));
                    TxtLrNo.setText(replyCmb.getString("lr_no"));
                    TxtIwo.setText(Integer.toString(replyCmb.getInt("iwo_id")));
                    IwoDate.setDate(new java.util.Date(replyCmb.getDate("iwo_date").getTime()));
                    TxtOrderNo.setText(Integer.toString(replyCmb.getInt("order_no")));
                    OrderDate.setDate(new java.util.Date(replyCmb.getDate("order_date").getTime()));
                    TxtDespatch.setText(replyCmb.getString("despatch_mode"));
                    TxtCustomer.setText(replyCmb.getString("name")+", "+replyCmb.getString("address"));
                } catch (SQLException ex) {
                    javax.swing.JOptionPane.showMessageDialog(null, ex.toString());
                }

            }
        });
    }
}
